<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" >
    <meta http-equiv="X-UA-Compatible" content="IE=11,IE=10,IE=9,IE=8" >
    <meta name="baidu-site-verification" content="iJemnNsV3M" />
    
    <title>职工管理系统 | lupin</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" >
    <meta name="keywords" content="全栈" >
    <meta name="description" content="全栈" >

    
    <link rel="shortcut icon" href="/img/favicon.png" >
    
    
<link rel="stylesheet" href="/css/style.css">

    <!--[if lt IE 9]>
    
<script src="/js/html5.js"></script>

    <![endif]-->
    
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?d5ebf515ab530cfbdda5f5c85093fb41";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>


<meta name="generator" content="Hexo 4.2.0"></head>

<body class="home">
    <!--[if lt IE 9]>
    <div class="browsehappy">
        当前网页 <strong>不支持</strong>
        你正在使用的浏览器. 为了正常的访问, 请 <a href="http://browsehappy.com/" target="_blank" rel="noopener">升级你的浏览器</a>.
    </div>
    <![endif]-->

    <!-- 博客头部 -->
    <header class="header">
    <section class="container header-main">
        <div class="logo">
            <a href="/">
                <div class="cover">
                    <span class="name">lupin</span>
                    <span class="description">博客</span>
                </div>
            </a>
        </div>
        <div class="dropnav icon-paragraph-justify" id="ALLVAR__btnDropNav"></div>
        <ul class="menu hidden" id="ALLVAR__menu">
            
            <li rel="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/index.html" class="item ">
                <a href="/" title="首页" class="icon-home">&nbsp;首页</a>
            </li>
            
            <li rel="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/index.html" class="item ">
                <a href="/lab/" title="实验室" class="icon-lab">&nbsp;实验室</a>
            </li>
            
            <li rel="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/index.html" class="item ">
                <a href="/about/" title="关于" class="icon-about">&nbsp;关于</a>
            </li>
            
        </ul>
        <div class="profile clearfix">
            <div class="feeds fl">
                
                
                <p class="links">
                    
                        <a href="https://gitee.com/Shr9527" target="_blank">码云</a>
                        |
                    
                        <a href="https://gitee.com" target="_blank">Hosted by Gitee Pages</a>
                        
                    
                </p>
                <p class="sns">
                    
                    <a href="javascript: void(0);" class="wechat">
                        <b>■</b>
                        公众号
                        <span class="popover">
                            <img src="/img/wechat_mp.jpg" width="120" height="120" alt="我的微信订阅号">
                            <i class="arrow"></i>
                        </span>
                    </a>
                </p>
                
            </div>
            <div class="avatar fr">
                <img src="/img/favicon.png" alt="avatar" title="AllVar" >
            </div>
        </div>
    </section>
</header>


    <!-- 博客正文 -->
    <div class="container body clearfix">
        <section class="content">
            <div class="content-main widget">
                <!-- 文章页 -->
<!-- 文章 -->
<article class="post article">
    <header class="text-center">
        <h3 class="post-title"><span>职工管理系统</span></h3>
    </header>
    <p class="post-meta text-center">
         发表于
        <time datetime="2020-04-29T09:27:00.000Z">2020-04-29</time>
    </p>
    <div class="post-content">
        <h3 id="职工管理系统"><a href="#职工管理系统" class="headerlink" title="职工管理系统"></a>职工管理系统</h3><h4 id="1、管理系统需求"><a href="#1、管理系统需求" class="headerlink" title="1、管理系统需求"></a>1、管理系统需求</h4><a id="more"></a>

<p>职工管理系统可以用来管理公司内所有员工的信息</p>
<p>本教程主要利用C++来实现一个基于多态的职工管理系统</p>
<p>公司中职工分为三类：普通员工、经理、老板，显示信息时，需要显示职工编号、职工姓名、职工岗位、以及职责</p>
<p>普通员工职责：完成经理交给的任务</p>
<p>经理职责：完成老板交给的任务，并下发任务给员工</p>
<p>老板职责：管理公司所有事务</p>
<p>管理系统中需要实现的功能如下：</p>
<ul>
<li>退出管理程序：退出当前管理系统</li>
</ul>
<ul>
<li>增加职工信息：实现批量添加职工功能，将信息录入到文件中，职工信息为：职工编号、姓名、部门编号</li>
<li>显示职工信息：显示公司内部所有职工的信息</li>
<li>删除离职职工：按照编号删除指定的职工</li>
<li>修改职工信息：按照编号修改职工个人信息</li>
<li>查找职工信息：按照职工的编号或者职工的姓名进行查找相关的人员信息</li>
<li>按照编号排序：按照职工编号，进行排序，排序规则由用户指定</li>
<li>清空所有文档：清空文件中记录的所有职工信息 （清空前需要再次确认，防止误删）</li>
</ul>
<p>系统界面效果图如下：</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546511409198.png" alt></p>
<p>需根据用户不同的选择，完成不同的功能！</p>
<h4 id="2、创建项目"><a href="#2、创建项目" class="headerlink" title="2、创建项目"></a>2、创建项目</h4><p>创建项目步骤如下：</p>
<ul>
<li>创建新项目</li>
<li>添加文件</li>
</ul>
<h5 id="2-1-创建项目"><a href="#2-1-创建项目" class="headerlink" title="2.1 创建项目"></a>2.1 创建项目</h5><p>打开vs2017后，点击创建新项目，创建新的C++项目</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1544151201465.png" alt></p>
<p>填写项目名称以及项目路径，点击确定</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546349209805.png" alt></p>
<h5 id="2-2-添加文件"><a href="#2-2-添加文件" class="headerlink" title="2.2 添加文件"></a>2.2 添加文件</h5><p>右键源文件，进行添加文件操作</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546349360960.png" alt></p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546349421496.png" alt></p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546349488752.png" alt></p>
<p>至此，项目已创建完毕</p>
<h4 id="3、创建管理类"><a href="#3、创建管理类" class="headerlink" title="3、创建管理类"></a>3、创建管理类</h4><p>​    管理类负责的内容如下：</p>
<ul>
<li>与用户的沟通菜单界面</li>
<li>对职工增删改查的操作</li>
<li>与文件的读写交互</li>
</ul>
<h5 id="3-1创建文件"><a href="#3-1创建文件" class="headerlink" title="3.1创建文件"></a>3.1创建文件</h5><p>在头文件和源文件的文件夹下分别创建workerManager.h 和 workerManager.cpp文件</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546349904944.png" alt></p>
<h5 id="3-2-头文件实现"><a href="#3-2-头文件实现" class="headerlink" title="3.2 头文件实现"></a>3.2 头文件实现</h5><p>在workerManager.h中设计管理类</p>
<p>代码如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">pragma</span> once</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">WorkerManager</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"></span><br><span class="line">	<span class="comment">//构造函数</span></span><br><span class="line">	WorkerManager();</span><br><span class="line"></span><br><span class="line">	<span class="comment">//析构函数</span></span><br><span class="line">	~WorkerManager();</span><br><span class="line"></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h5 id="3-3-源文件实现"><a href="#3-3-源文件实现" class="headerlink" title="3.3 源文件实现"></a>3.3 源文件实现</h5><p>在workerManager.cpp中将构造和析构函数空实现补全</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"workerManager.h"</span></span></span><br><span class="line"></span><br><span class="line">WorkerManager::WorkerManager()</span><br><span class="line">&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">WorkerManager::~WorkerManager()</span><br><span class="line">&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>至此职工管理类以创建完毕</p>
<h4 id="4、菜单功能"><a href="#4、菜单功能" class="headerlink" title="4、菜单功能"></a>4、菜单功能</h4><p>功能描述：与用户的沟通界面</p>
<h5 id="4-1-添加成员函数"><a href="#4-1-添加成员函数" class="headerlink" title="4.1 添加成员函数"></a>4.1 添加成员函数</h5><p>在管理类workerManager.h中添加成员函数  <code>void Show_Menu();</code></p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546351543942.png" alt></p>
<h5 id="4-2-菜单功能实现"><a href="#4-2-菜单功能实现" class="headerlink" title="4.2 菜单功能实现"></a>4.2 菜单功能实现</h5><p>在管理类workerManager.cpp中实现  Show_Menu()函数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">WorkerManager::Show_Menu</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"********************************************"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"*********  欢迎使用职工管理系统！ **********"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"*************  0.退出管理程序  *************"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"*************  1.增加职工信息  *************"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"*************  2.显示职工信息  *************"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"*************  3.删除离职职工  *************"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"*************  4.修改职工信息  *************"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"*************  5.查找职工信息  *************"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"*************  6.按照编号排序  *************"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"*************  7.清空所有文档  *************"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"********************************************"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h5 id="4-3-测试菜单功能"><a href="#4-3-测试菜单功能" class="headerlink" title="4.3 测试菜单功能"></a>4.3 测试菜单功能</h5><p>在职工管理系统.cpp中测试菜单功能</p>
<p>代码：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"workerManager.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	WorkerManager wm;</span><br><span class="line"></span><br><span class="line">	wm.Show_Menu();</span><br><span class="line"></span><br><span class="line">	system(<span class="string">"pause"</span>);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>运行效果如图：</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546352771191.png" alt></p>
<h4 id="5、退出功能"><a href="#5、退出功能" class="headerlink" title="5、退出功能"></a>5、退出功能</h4><h5 id="5-1-提供功能接口"><a href="#5-1-提供功能接口" class="headerlink" title="5.1  提供功能接口"></a>5.1  提供功能接口</h5><p>在main函数中提供分支选择，提供每个功能接口</p>
<p>代码：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	WorkerManager wm;</span><br><span class="line">	<span class="keyword">int</span> choice = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span> (<span class="literal">true</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="comment">//展示菜单</span></span><br><span class="line">		wm.Show_Menu();</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"请输入您的选择:"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		<span class="built_in">cin</span> &gt;&gt; choice;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">switch</span> (choice)</span><br><span class="line">		&#123;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">0</span>: <span class="comment">//退出系统</span></span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">1</span>: <span class="comment">//添加职工</span></span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">2</span>: <span class="comment">//显示职工</span></span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">3</span>: <span class="comment">//删除职工</span></span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">4</span>: <span class="comment">//修改职工</span></span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">5</span>: <span class="comment">//查找职工</span></span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">6</span>: <span class="comment">//排序职工</span></span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">7</span>: <span class="comment">//清空文件</span></span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			system(<span class="string">"cls"</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	system(<span class="string">"pause"</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h5 id="5-2-实现退出功能"><a href="#5-2-实现退出功能" class="headerlink" title="5.2 实现退出功能"></a>5.2 实现退出功能</h5><p>在workerManager.h中提供退出系统的成员函数 <code>void exitSystem();</code></p>
<p>在workerManager.cpp中提供具体的功能实现</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">WorkerManager::exitSystem</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"欢迎下次使用"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	system(<span class="string">"pause"</span>);</span><br><span class="line">	<span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h5 id="5-3测试功能"><a href="#5-3测试功能" class="headerlink" title="5.3测试功能"></a>5.3测试功能</h5><p>在main函数分支 0  选项中，调用退出程序的接口</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546353199424.png" alt></p>
<p>运行测试效果如图：</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546353155490.png" alt></p>
<h4 id="6、创建职工类"><a href="#6、创建职工类" class="headerlink" title="6、创建职工类"></a>6、创建职工类</h4><h5 id="6-1-创建职工抽象类"><a href="#6-1-创建职工抽象类" class="headerlink" title="6.1 创建职工抽象类"></a>6.1 创建职工抽象类</h5><p>职工的分类为：普通员工、经理、老板</p>
<p>将三种职工抽象到一个类（worker）中,利用多态管理不同职工种类</p>
<p>职工的属性为：职工编号、职工姓名、职工所在部门编号</p>
<p>职工的行为为：岗位职责信息描述，获取岗位名称</p>
<p>头文件文件夹下  创建文件worker.h  文件并且添加如下代码：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">pragma</span> once</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//职工抽象基类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Worker</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"></span><br><span class="line">	<span class="comment">//显示个人信息</span></span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">showInfo</span><span class="params">()</span> </span>= <span class="number">0</span>;</span><br><span class="line">	<span class="comment">//获取岗位名称</span></span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="built_in">string</span> <span class="title">getDeptName</span><span class="params">()</span> </span>= <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">int</span> m_Id; <span class="comment">//职工编号</span></span><br><span class="line">	<span class="built_in">string</span> m_Name; <span class="comment">//职工姓名</span></span><br><span class="line">	<span class="keyword">int</span> m_DeptId; <span class="comment">//职工所在部门名称编号</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h5 id="6-2-创建普通员工类"><a href="#6-2-创建普通员工类" class="headerlink" title="6.2 创建普通员工类"></a>6.2 创建普通员工类</h5><p>普通员工类<strong>继承</strong>职工抽象类，并重写父类中纯虚函数</p>
<p>在头文件和源文件的文件夹下分别创建employee.h 和 employee.cpp文件</p>
<p>employee.h中代码如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">pragma</span> once </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"worker.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//员工类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Employee</span> :</span><span class="keyword">public</span> Worker</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"></span><br><span class="line">	<span class="comment">//构造函数</span></span><br><span class="line">	Employee(<span class="keyword">int</span> id, <span class="built_in">string</span> name, <span class="keyword">int</span> dId);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//显示个人信息</span></span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">showInfo</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//获取职工岗位名称</span></span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="built_in">string</span> <span class="title">getDeptName</span><span class="params">()</span></span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>employee.cpp中代码如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"employee.h"</span></span></span><br><span class="line"></span><br><span class="line">Employee::Employee(<span class="keyword">int</span> id, <span class="built_in">string</span> name, <span class="keyword">int</span> dId)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">this</span>-&gt;m_Id = id;</span><br><span class="line">	<span class="keyword">this</span>-&gt;m_Name = name;</span><br><span class="line">	<span class="keyword">this</span>-&gt;m_DeptId = dId;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Employee::showInfo</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"职工编号： "</span> &lt;&lt; <span class="keyword">this</span>-&gt;m_Id</span><br><span class="line">		&lt;&lt; <span class="string">" \t职工姓名： "</span> &lt;&lt; <span class="keyword">this</span>-&gt;m_Name</span><br><span class="line">		&lt;&lt; <span class="string">" \t岗位："</span> &lt;&lt; <span class="keyword">this</span>-&gt;getDeptName()</span><br><span class="line">		&lt;&lt; <span class="string">" \t岗位职责：完成经理交给的任务"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="built_in">string</span> <span class="title">Employee::getDeptName</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">return</span> <span class="built_in">string</span>(<span class="string">"员工"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h5 id="6-3-创建经理类"><a href="#6-3-创建经理类" class="headerlink" title="6.3 创建经理类"></a>6.3 创建经理类</h5><p>经理类<strong>继承</strong>职工抽象类，并重写父类中纯虚函数，和普通员工类似</p>
<p>在头文件和源文件的文件夹下分别创建manager.h 和 manager.cpp文件</p>
<p>manager.h中代码如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">pragma</span> once</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"worker.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//经理类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Manager</span> :</span><span class="keyword">public</span> Worker</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"></span><br><span class="line">	Manager(<span class="keyword">int</span> id, <span class="built_in">string</span> name, <span class="keyword">int</span> dId);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//显示个人信息</span></span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">showInfo</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//获取职工岗位名称</span></span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="built_in">string</span> <span class="title">getDeptName</span><span class="params">()</span></span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>manager.cpp中代码如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"manager.h"</span></span></span><br><span class="line"></span><br><span class="line">Manager::Manager(<span class="keyword">int</span> id, <span class="built_in">string</span> name, <span class="keyword">int</span> dId)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">this</span>-&gt;m_Id = id;</span><br><span class="line">	<span class="keyword">this</span>-&gt;m_Name = name;</span><br><span class="line">	<span class="keyword">this</span>-&gt;m_DeptId = dId;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Manager::showInfo</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"职工编号： "</span> &lt;&lt; <span class="keyword">this</span>-&gt;m_Id</span><br><span class="line">		&lt;&lt; <span class="string">" \t职工姓名： "</span> &lt;&lt; <span class="keyword">this</span>-&gt;m_Name</span><br><span class="line">		&lt;&lt; <span class="string">" \t岗位："</span> &lt;&lt; <span class="keyword">this</span>-&gt;getDeptName()</span><br><span class="line">		&lt;&lt; <span class="string">" \t岗位职责：完成老板交给的任务,并下发任务给员工"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="built_in">string</span> <span class="title">Manager::getDeptName</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">return</span> <span class="built_in">string</span>(<span class="string">"经理"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h5 id="6-4-创建老板类"><a href="#6-4-创建老板类" class="headerlink" title="6.4 创建老板类"></a>6.4 创建老板类</h5><p>老板类<strong>继承</strong>职工抽象类，并重写父类中纯虚函数，和普通员工类似</p>
<p>在头文件和源文件的文件夹下分别创建boss.h 和 boss.cpp文件</p>
<p>boss.h中代码如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">pragma</span> once</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"worker.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//老板类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Boss</span> :</span><span class="keyword">public</span> Worker</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"></span><br><span class="line">	Boss(<span class="keyword">int</span> id, <span class="built_in">string</span> name, <span class="keyword">int</span> dId);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//显示个人信息</span></span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">showInfo</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//获取职工岗位名称</span></span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="built_in">string</span> <span class="title">getDeptName</span><span class="params">()</span></span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>boss.cpp中代码如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"boss.h"</span></span></span><br><span class="line"></span><br><span class="line">Boss::Boss(<span class="keyword">int</span> id, <span class="built_in">string</span> name, <span class="keyword">int</span> dId)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">this</span>-&gt;m_Id = id;</span><br><span class="line">	<span class="keyword">this</span>-&gt;m_Name = name;</span><br><span class="line">	<span class="keyword">this</span>-&gt;m_DeptId = dId;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Boss::showInfo</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"职工编号： "</span> &lt;&lt; <span class="keyword">this</span>-&gt;m_Id</span><br><span class="line">		&lt;&lt; <span class="string">" \t职工姓名： "</span> &lt;&lt; <span class="keyword">this</span>-&gt;m_Name</span><br><span class="line">		&lt;&lt; <span class="string">" \t岗位："</span> &lt;&lt; <span class="keyword">this</span>-&gt;getDeptName()</span><br><span class="line">		&lt;&lt; <span class="string">" \t岗位职责：管理公司所有事务"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="built_in">string</span> <span class="title">Boss::getDeptName</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">return</span> <span class="built_in">string</span>(<span class="string">"总裁"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h5 id="6-5-测试多态"><a href="#6-5-测试多态" class="headerlink" title="6.5 测试多态"></a>6.5 测试多态</h5><p>在职工管理系统.cpp中添加测试函数，并且运行能够产生多态</p>
<p>测试代码如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"worker.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"employee.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"manager.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"boss.h"</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">test</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	Worker * worker = <span class="literal">NULL</span>;</span><br><span class="line">	worker = <span class="keyword">new</span> Employee(<span class="number">1</span>, <span class="string">"张三"</span>, <span class="number">1</span>);</span><br><span class="line">	worker-&gt;showInfo();</span><br><span class="line">	<span class="keyword">delete</span> worker;</span><br><span class="line">	</span><br><span class="line">	worker = <span class="keyword">new</span> Manager(<span class="number">2</span>, <span class="string">"李四"</span>, <span class="number">2</span>);</span><br><span class="line">	worker-&gt;showInfo();</span><br><span class="line">	<span class="keyword">delete</span> worker;</span><br><span class="line"></span><br><span class="line">	worker = <span class="keyword">new</span> Boss(<span class="number">3</span>, <span class="string">"王五"</span>, <span class="number">3</span>);</span><br><span class="line">	worker-&gt;showInfo();</span><br><span class="line">	<span class="keyword">delete</span> worker;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>运行效果如图：</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546398236081.png" alt></p>
<p>测试成功后，测试代码可以注释保留，或者选择删除</p>
<h4 id="7、添加职工"><a href="#7、添加职工" class="headerlink" title="7、添加职工"></a>7、添加职工</h4><p>功能描述：批量添加职工，并且保存到文件中</p>
<h5 id="7-1-功能分析"><a href="#7-1-功能分析" class="headerlink" title="7.1 功能分析"></a>7.1 功能分析</h5><p>分析：</p>
<p>用户在批量创建时，可能会创建不同种类的职工</p>
<p>如果想将所有不同种类的员工都放入到一个数组中，可以将所有员工的指针维护到一个数组里</p>
<p>如果想在程序中维护这个不定长度的数组，可以将数组创建到堆区，并利用Worker **的指针维护</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546399491099.png" alt></p>
<h5 id="7-2-功能实现"><a href="#7-2-功能实现" class="headerlink" title="7.2 功能实现"></a>7.2 功能实现</h5><p>在WokerManager.h头文件中添加成员属性 代码：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//记录文件中的人数个数</span></span><br><span class="line"><span class="keyword">int</span> m_EmpNum;</span><br><span class="line"></span><br><span class="line"><span class="comment">//员工数组的指针</span></span><br><span class="line">Worker ** m_EmpArray;</span><br></pre></td></tr></table></figure>



<p>在WorkerManager构造函数中初始化属性</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">WorkerManager::WorkerManager()</span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">//初始化人数</span></span><br><span class="line">	<span class="keyword">this</span>-&gt;m_EmpNum = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//初始化数组指针</span></span><br><span class="line">	<span class="keyword">this</span>-&gt;m_EmpArray = <span class="literal">NULL</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>在workerManager.h中添加成员函数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//增加职工</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Add_Emp</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>



<p>workerManager.cpp中实现该函数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//增加职工</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">WorkerManager::Add_Emp</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"请输入增加职工数量： "</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">int</span> addNum = <span class="number">0</span>;</span><br><span class="line">	<span class="built_in">cin</span> &gt;&gt; addNum;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (addNum &gt; <span class="number">0</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="comment">//计算新空间大小</span></span><br><span class="line">		<span class="keyword">int</span> newSize = <span class="keyword">this</span>-&gt;m_EmpNum + addNum;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//开辟新空间</span></span><br><span class="line">		Worker ** newSpace = <span class="keyword">new</span> Worker*[newSize];</span><br><span class="line"></span><br><span class="line">		<span class="comment">//将原空间下内容存放到新空间下</span></span><br><span class="line">		<span class="keyword">if</span> (<span class="keyword">this</span>-&gt;m_EmpArray != <span class="literal">NULL</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>-&gt;m_EmpNum; i++)</span><br><span class="line">			&#123;</span><br><span class="line">				newSpace[i] = <span class="keyword">this</span>-&gt;m_EmpArray[i];</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//输入新数据</span></span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; addNum; i++)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">int</span> id;</span><br><span class="line">			<span class="built_in">string</span> name;</span><br><span class="line">			<span class="keyword">int</span> dSelect;</span><br><span class="line"></span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"请输入第 "</span> &lt;&lt; i + <span class="number">1</span> &lt;&lt; <span class="string">" 个新职工编号："</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">			<span class="built_in">cin</span> &gt;&gt; id;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"请输入第 "</span> &lt;&lt; i + <span class="number">1</span> &lt;&lt; <span class="string">" 个新职工姓名："</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">			<span class="built_in">cin</span> &gt;&gt; name;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"请选择该职工的岗位："</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"1、普通职工"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"2、经理"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"3、老板"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">			<span class="built_in">cin</span> &gt;&gt; dSelect;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">			Worker * worker = <span class="literal">NULL</span>;</span><br><span class="line">			<span class="keyword">switch</span> (dSelect)</span><br><span class="line">			&#123;</span><br><span class="line">			<span class="keyword">case</span> <span class="number">1</span>: <span class="comment">//普通员工</span></span><br><span class="line">				worker = <span class="keyword">new</span> Employee(id, name, <span class="number">1</span>);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">case</span> <span class="number">2</span>: <span class="comment">//经理</span></span><br><span class="line">				worker = <span class="keyword">new</span> Manager(id, name, <span class="number">2</span>);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">case</span> <span class="number">3</span>:  <span class="comment">//老板</span></span><br><span class="line">				worker = <span class="keyword">new</span> Boss(id, name, <span class="number">3</span>);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">default</span>:</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">			newSpace[<span class="keyword">this</span>-&gt;m_EmpNum + i] = worker;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//释放原有空间</span></span><br><span class="line">		<span class="keyword">delete</span>[] <span class="keyword">this</span>-&gt;m_EmpArray;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//更改新空间的指向</span></span><br><span class="line">		<span class="keyword">this</span>-&gt;m_EmpArray = newSpace;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//更新新的个数</span></span><br><span class="line">		<span class="keyword">this</span>-&gt;m_EmpNum = newSize;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//提示信息</span></span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"成功添加"</span> &lt;&lt; addNum &lt;&lt; <span class="string">"名新职工！"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"输入有误"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	system(<span class="string">"pause"</span>);</span><br><span class="line">	system(<span class="string">"cls"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>在WorkerManager.cpp的析构函数中，释放堆区数据</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">WorkerManager::~WorkerManager()</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>-&gt;m_EmpArray != <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">delete</span>[] <span class="keyword">this</span>-&gt;m_EmpArray;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h5 id="7-3-测试添加"><a href="#7-3-测试添加" class="headerlink" title="7.3 测试添加"></a>7.3 测试添加</h5><p>在main函数分支 1  选项中，调用添加职工接口</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546401705277.png" alt></p>
<p>效果如图：</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546401763461.png" alt></p>
<p>至此，添加职工到程序中功能实现完毕</p>
<h4 id="8、文件交互-写文件"><a href="#8、文件交互-写文件" class="headerlink" title="8、文件交互 - 写文件"></a>8、文件交互 - 写文件</h4><p>功能描述：对文件进行读写</p>
<p>​    在上一个添加功能中，我们只是将所有的数据添加到了内存中，一旦程序结束就无法保存了</p>
<p>​    因此文件管理类中需要一个与文件进行交互的功能，对于文件进行读写操作</p>
<h5 id="8-1-设定文件路径"><a href="#8-1-设定文件路径" class="headerlink" title="8.1 设定文件路径"></a>8.1 设定文件路径</h5><p>首先我们将文件路径，在workerManager.h中添加宏常量,并且包含头文件 fstream</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;fstream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span>  FILENAME <span class="meta-string">"empFile.txt"</span></span></span><br></pre></td></tr></table></figure>



<h5 id="8-2-成员函数声明"><a href="#8-2-成员函数声明" class="headerlink" title="8.2 成员函数声明"></a>8.2 成员函数声明</h5><p>在workerManager.h中类里添加成员函数 <code>void save()</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//保存文件</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">save</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>



<h5 id="8-3-保存文件功能实现"><a href="#8-3-保存文件功能实现" class="headerlink" title="8.3 保存文件功能实现"></a>8.3 保存文件功能实现</h5><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">WorkerManager::save</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	ofstream ofs;</span><br><span class="line">	ofs.<span class="built_in">open</span>(FILENAME, ios::out);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>-&gt;m_EmpNum; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		ofs &lt;&lt; <span class="keyword">this</span>-&gt;m_EmpArray[i]-&gt;m_Id &lt;&lt; <span class="string">" "</span> </span><br><span class="line">			&lt;&lt; <span class="keyword">this</span>-&gt;m_EmpArray[i]-&gt;m_Name &lt;&lt; <span class="string">" "</span> </span><br><span class="line">			&lt;&lt; <span class="keyword">this</span>-&gt;m_EmpArray[i]-&gt;m_DeptId &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	ofs.<span class="built_in">close</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h5 id="8-4-保存文件功能测试"><a href="#8-4-保存文件功能测试" class="headerlink" title="8.4 保存文件功能测试"></a>8.4 保存文件功能测试</h5><p>在添加职工功能中添加成功后添加保存文件函数</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546432469465.png" alt></p>
<p>再次运行代码，添加职工</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546401763461.png" alt></p>
<p>同级目录下多出文件，并且保存了添加的信息</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546432343078.png" alt></p>
<h4 id="9、文件交互-读文件"><a href="#9、文件交互-读文件" class="headerlink" title="9、文件交互 - 读文件"></a>9、文件交互 - 读文件</h4><p>功能描述：将文件中的内容读取到程序中</p>
<p>虽然我们实现了添加职工后保存到文件的操作，但是每次开始运行程序，并没有将文件中数据读取到程序中</p>
<p>而我们的程序功能中还有清空文件的需求</p>
<p>因此构造函数初始化数据的情况分为三种</p>
<ol>
<li>第一次使用，文件未创建</li>
<li>文件存在，但是数据被用户清空</li>
<li>文件存在，并且保存职工的所有数据</li>
</ol>
<h5 id="9-1-文件未创建"><a href="#9-1-文件未创建" class="headerlink" title="9.1 文件未创建"></a>9.1 文件未创建</h5><p>在workerManager.h中添加新的成员属性 m_FileIsEmpty标志文件是否为空</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//标志文件是否为空</span></span><br><span class="line"><span class="keyword">bool</span> m_FileIsEmpty;</span><br></pre></td></tr></table></figure>



<p>修改WorkerManager.cpp中构造函数代码</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">WorkerManager::WorkerManager()</span><br><span class="line">&#123;</span><br><span class="line">	ifstream ifs;</span><br><span class="line">	ifs.<span class="built_in">open</span>(FILENAME, ios::in);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//文件不存在情况</span></span><br><span class="line">	<span class="keyword">if</span> (!ifs.is_open())</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"文件不存在"</span> &lt;&lt; <span class="built_in">endl</span>; <span class="comment">//测试输出</span></span><br><span class="line">		<span class="keyword">this</span>-&gt;m_EmpNum = <span class="number">0</span>;  <span class="comment">//初始化人数</span></span><br><span class="line">		<span class="keyword">this</span>-&gt;m_FileIsEmpty = <span class="literal">true</span>; <span class="comment">//初始化文件为空标志</span></span><br><span class="line">		<span class="keyword">this</span>-&gt;m_EmpArray = <span class="literal">NULL</span>; <span class="comment">//初始化数组</span></span><br><span class="line">		ifs.<span class="built_in">close</span>(); <span class="comment">//关闭文件</span></span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>删除文件后，测试文件不存在时初始化数据功能</p>
<h5 id="9-2-文件存在且数据为空"><a href="#9-2-文件存在且数据为空" class="headerlink" title="9.2 文件存在且数据为空"></a>9.2 文件存在且数据为空</h5><p>在workerManager.cpp中的构造函数追加代码：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//文件存在，并且没有记录</span></span><br><span class="line"><span class="keyword">char</span> ch;</span><br><span class="line">ifs &gt;&gt; ch;</span><br><span class="line"><span class="keyword">if</span> (ifs.eof())</span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"文件为空!"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="keyword">this</span>-&gt;m_EmpNum = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">this</span>-&gt;m_FileIsEmpty = <span class="literal">true</span>;</span><br><span class="line">	<span class="keyword">this</span>-&gt;m_EmpArray = <span class="literal">NULL</span>;</span><br><span class="line">	ifs.<span class="built_in">close</span>();</span><br><span class="line">	<span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>追加代码位置如图：</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546435197575.png" alt></p>
<p>将文件创建后清空文件内容，并测试该情况下初始化功能</p>
<p>我们发现文件不存在或者为空清空 m_FileIsEmpty 判断文件是否为空的标志都为真，那何时为假？</p>
<p>成功添加职工后，应该更改文件不为空的标志</p>
<p>在<code>void WorkerManager::Add_Emp()</code>成员函数中添加：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//更新职工不为空标志</span></span><br><span class="line"><span class="keyword">this</span>-&gt;m_FileIsEmpty = <span class="literal">false</span>;</span><br></pre></td></tr></table></figure>

<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546656256176.png" alt></p>
<h5 id="9-3-文件存在且保存职工数据"><a href="#9-3-文件存在且保存职工数据" class="headerlink" title="9.3 文件存在且保存职工数据"></a>9.3 文件存在且保存职工数据</h5><h6 id="9-3-1-获取记录的职工人数"><a href="#9-3-1-获取记录的职工人数" class="headerlink" title="9.3.1 获取记录的职工人数"></a>9.3.1 获取记录的职工人数</h6><p>在workerManager.h中添加成员函数 <code>int get_EmpNum();</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//统计人数</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">get_EmpNum</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>

<p>workerManager.cpp中实现</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">WorkerManager::get_EmpNum</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	ifstream ifs;</span><br><span class="line">	ifs.<span class="built_in">open</span>(FILENAME, ios::in);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">int</span> id;</span><br><span class="line">	<span class="built_in">string</span> name;</span><br><span class="line">	<span class="keyword">int</span> dId;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">int</span> num = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">while</span> (ifs &gt;&gt; id &amp;&amp; ifs &gt;&gt; name &amp;&amp; ifs &gt;&gt; dId)</span><br><span class="line">	&#123;</span><br><span class="line">        <span class="comment">//记录人数</span></span><br><span class="line">		num++;</span><br><span class="line">	&#125;</span><br><span class="line">	ifs.<span class="built_in">close</span>();</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> num;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在workerManager.cpp构造函数中继续追加代码：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> num =  <span class="keyword">this</span>-&gt;get_EmpNum();</span><br><span class="line"><span class="built_in">cout</span> &lt;&lt; <span class="string">"职工个数为："</span> &lt;&lt; num &lt;&lt; <span class="built_in">endl</span>;  <span class="comment">//测试代码</span></span><br><span class="line"><span class="keyword">this</span>-&gt;m_EmpNum = num;  <span class="comment">//更新成员属性</span></span><br></pre></td></tr></table></figure>



<p>手动添加一些职工数据，测试获取职工数量函数</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546436429055.png" alt></p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546436385793.png" alt></p>
<h6 id="9-3-2-初始化数组"><a href="#9-3-2-初始化数组" class="headerlink" title="9.3.2 初始化数组"></a>9.3.2 初始化数组</h6><p>根据职工的数据以及职工数据，初始化workerManager中的Worker ** m_EmpArray 指针</p>
<p>在WorkerManager.h中添加成员函数  <code>void init_Emp();</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//初始化员工</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init_Emp</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>



<p>在WorkerManager.cpp中实现</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">WorkerManager::init_Emp</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	ifstream ifs;</span><br><span class="line">	ifs.<span class="built_in">open</span>(FILENAME, ios::in);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">int</span> id;</span><br><span class="line">	<span class="built_in">string</span> name;</span><br><span class="line">	<span class="keyword">int</span> dId;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">int</span> index = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span> (ifs &gt;&gt; id &amp;&amp; ifs &gt;&gt; name &amp;&amp; ifs &gt;&gt; dId)</span><br><span class="line">	&#123;</span><br><span class="line">		Worker * worker = <span class="literal">NULL</span>;</span><br><span class="line">		<span class="comment">//根据不同的部门Id创建不同对象</span></span><br><span class="line">		<span class="keyword">if</span> (dId == <span class="number">1</span>)  <span class="comment">// 1普通员工</span></span><br><span class="line">		&#123;</span><br><span class="line">			worker = <span class="keyword">new</span> Employee(id, name, dId);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">if</span> (dId == <span class="number">2</span>) <span class="comment">//2经理</span></span><br><span class="line">		&#123;</span><br><span class="line">			worker = <span class="keyword">new</span> Manager(id, name, dId);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> <span class="comment">//总裁</span></span><br><span class="line">		&#123;</span><br><span class="line">			worker = <span class="keyword">new</span> Boss(id, name, dId);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//存放在数组中</span></span><br><span class="line">		<span class="keyword">this</span>-&gt;m_EmpArray[index] = worker;</span><br><span class="line">		index++;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>在workerManager.cpp构造函数中追加代码</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//根据职工数创建数组</span></span><br><span class="line"><span class="keyword">this</span>-&gt;m_EmpArray = <span class="keyword">new</span> Worker *[<span class="keyword">this</span>-&gt;m_EmpNum];</span><br><span class="line"><span class="comment">//初始化职工</span></span><br><span class="line">init_Emp();</span><br><span class="line"></span><br><span class="line"><span class="comment">//测试代码</span></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m_EmpNum; i++)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"职工号： "</span> &lt;&lt; <span class="keyword">this</span>-&gt;m_EmpArray[i]-&gt;m_Id</span><br><span class="line">		&lt;&lt; <span class="string">" 职工姓名： "</span> &lt;&lt; <span class="keyword">this</span>-&gt;m_EmpArray[i]-&gt;m_Name</span><br><span class="line">		&lt;&lt; <span class="string">" 部门编号： "</span> &lt;&lt; <span class="keyword">this</span>-&gt;m_EmpArray[i]-&gt;m_DeptId &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>运行程序，测试从文件中获取的数据</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546436938152.png" alt></p>
<p>至此初始化数据功能完毕，测试代码可以注释或删除掉！</p>
<h4 id="10、显示职工"><a href="#10、显示职工" class="headerlink" title="10、显示职工"></a>10、显示职工</h4><p>功能描述：显示当前所有职工信息</p>
<h6 id="10-1-显示职工函数声明"><a href="#10-1-显示职工函数声明" class="headerlink" title="10.1 显示职工函数声明"></a>10.1 显示职工函数声明</h6><p>在workerManager.h中添加成员函数  <code>void Show_Emp();</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//显示职工</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Show_Emp</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>



<h6 id="10-2-显示职工函数实现"><a href="#10-2-显示职工函数实现" class="headerlink" title="10.2 显示职工函数实现"></a>10.2 显示职工函数实现</h6><p>在workerManager.cpp中实现成员函数 <code>void Show_Emp();</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//显示职工</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">WorkerManager::Show_Emp</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>-&gt;m_FileIsEmpty)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"文件不存在或记录为空！"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m_EmpNum; i++)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="comment">//利用多态调用接口</span></span><br><span class="line">			<span class="keyword">this</span>-&gt;m_EmpArray[i]-&gt;showInfo();</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	system(<span class="string">"pause"</span>);</span><br><span class="line">	system(<span class="string">"cls"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h6 id="10-3-测试显示职工"><a href="#10-3-测试显示职工" class="headerlink" title="10.3 测试显示职工"></a>10.3 测试显示职工</h6><p>在main函数分支 2  选项中，调用显示职工接口</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546497336465.png" alt></p>
<p>测试时分别测试 文件为空和文件不为空两种情况</p>
<p>测试效果：</p>
<p>测试1-文件不存在或者为空情况</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546497082135.png" alt></p>
<p>测试2 - 文件存在且有记录情况</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546496947671.png" alt></p>
<p>测试完毕，至此，显示所有职工信息功能实现</p>
<h4 id="11、删除职工"><a href="#11、删除职工" class="headerlink" title="11、删除职工"></a>11、删除职工</h4><p>功能描述：按照职工的编号进行删除职工操作</p>
<h6 id="11-1-删除职工函数声明"><a href="#11-1-删除职工函数声明" class="headerlink" title="11.1 删除职工函数声明"></a>11.1 删除职工函数声明</h6><p>在workerManager.h中添加成员函数  <code>void Del_Emp();</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//删除职工</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Del_Emp</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>



<h6 id="11-2-职工是否存在函数声明"><a href="#11-2-职工是否存在函数声明" class="headerlink" title="11.2 职工是否存在函数声明"></a>11.2 职工是否存在函数声明</h6><p>很多功能都需要用到根据职工是否存在来进行操作如：删除职工、修改职工、查找职工</p>
<p>因此添加该公告函数，以便后续调用</p>
<p>在workerManager.h中添加成员函数  <code>int IsExist(int id);</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//按照职工编号判断职工是否存在,若存在返回职工在数组中位置，不存在返回-1</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">IsExist</span><span class="params">(<span class="keyword">int</span> id)</span></span>;</span><br></pre></td></tr></table></figure>



<h6 id="11-3-职工是否存在函数实现"><a href="#11-3-职工是否存在函数实现" class="headerlink" title="11.3 职工是否存在函数实现"></a>11.3 职工是否存在函数实现</h6><p>在workerManager.cpp中实现成员函数 <code>int IsExist(int id);</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">WorkerManager::IsExist</span><span class="params">(<span class="keyword">int</span> id)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> index = <span class="number">-1</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>-&gt;m_EmpNum; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (<span class="keyword">this</span>-&gt;m_EmpArray[i]-&gt;m_Id == id)</span><br><span class="line">		&#123;</span><br><span class="line">			index = i;</span><br><span class="line"></span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> index;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h6 id="11-4-删除职工函数实现"><a href="#11-4-删除职工函数实现" class="headerlink" title="11.4 删除职工函数实现"></a>11.4 删除职工函数实现</h6><p>在workerManager.cpp中实现成员函数 <code>void Del_Emp();</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//删除职工</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">WorkerManager::Del_Emp</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>-&gt;m_FileIsEmpty)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"文件不存在或记录为空！"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="comment">//按职工编号删除</span></span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"请输入想要删除的职工号："</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		<span class="keyword">int</span> id = <span class="number">0</span>;</span><br><span class="line">		<span class="built_in">cin</span> &gt;&gt; id;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">int</span> index = <span class="keyword">this</span>-&gt;IsExist(id);</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> (index != <span class="number">-1</span>)  <span class="comment">//说明index上位置数据需要删除</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">for</span> (<span class="keyword">int</span> i = index; i &lt; <span class="keyword">this</span>-&gt;m_EmpNum - <span class="number">1</span>; i++)</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="keyword">this</span>-&gt;m_EmpArray[i] = <span class="keyword">this</span>-&gt;m_EmpArray[i + <span class="number">1</span>];</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">this</span>-&gt;m_EmpNum--;</span><br><span class="line"></span><br><span class="line">			<span class="keyword">this</span>-&gt;save(); <span class="comment">//删除后数据同步到文件中</span></span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"删除成功！"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"删除失败，未找到该职工"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	system(<span class="string">"pause"</span>);</span><br><span class="line">	system(<span class="string">"cls"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h6 id="11-5-测试删除职工"><a href="#11-5-测试删除职工" class="headerlink" title="11.5 测试删除职工"></a>11.5 测试删除职工</h6><p>在main函数分支 3  选项中，调用删除职工接口</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546502698622.png" alt></p>
<p>测试1 - 删除不存在职工情况</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546500324196.png" alt></p>
<p>测试2 - 删除存在的职工情况</p>
<p>删除成功提示图：</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546500350526.png" alt></p>
<p>再次显示所有职工信息，确保已经删除</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546500361889.png" alt></p>
<p>查看文件中信息，再次核实员工已被完全删除</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546500383570.png" alt></p>
<p>至此，删除职工功能实现完毕！</p>
<h4 id="12、修改职工"><a href="#12、修改职工" class="headerlink" title="12、修改职工"></a>12、修改职工</h4><p>功能描述：能够按照职工的编号对职工信息进行修改并保存</p>
<h6 id="12-1-修改职工函数声明"><a href="#12-1-修改职工函数声明" class="headerlink" title="12.1 修改职工函数声明"></a>12.1 修改职工函数声明</h6><p>在workerManager.h中添加成员函数  <code>void Mod_Emp();</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//修改职工</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Mod_Emp</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>



<h6 id="12-2-修改职工函数实现"><a href="#12-2-修改职工函数实现" class="headerlink" title="12.2 修改职工函数实现"></a>12.2 修改职工函数实现</h6><p>在workerManager.cpp中实现成员函数 <code>void Mod_Emp();</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//修改职工</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">WorkerManager::Mod_Emp</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>-&gt;m_FileIsEmpty)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"文件不存在或记录为空！"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"请输入修改职工的编号："</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		<span class="keyword">int</span> id;</span><br><span class="line">		<span class="built_in">cin</span> &gt;&gt; id;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">int</span> ret = <span class="keyword">this</span>-&gt;IsExist(id);</span><br><span class="line">		<span class="keyword">if</span> (ret != <span class="number">-1</span>)</span><br><span class="line">		&#123; </span><br><span class="line">			<span class="comment">//查找到编号的职工</span></span><br><span class="line"></span><br><span class="line">			<span class="keyword">delete</span> <span class="keyword">this</span>-&gt;m_EmpArray[ret];</span><br><span class="line">			</span><br><span class="line">			<span class="keyword">int</span> newId = <span class="number">0</span>;</span><br><span class="line">			<span class="built_in">string</span> newName = <span class="string">""</span>;</span><br><span class="line">			<span class="keyword">int</span> dSelect = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"查到： "</span> &lt;&lt; id &lt;&lt; <span class="string">"号职工，请输入新职工号： "</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">			<span class="built_in">cin</span> &gt;&gt; newId;</span><br><span class="line"></span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"请输入新姓名： "</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">			<span class="built_in">cin</span> &gt;&gt; newName;</span><br><span class="line"></span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"请输入岗位： "</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"1、普通职工"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"2、经理"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"3、老板"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">			<span class="built_in">cin</span> &gt;&gt; dSelect;</span><br><span class="line"></span><br><span class="line">			Worker * worker = <span class="literal">NULL</span>;</span><br><span class="line">			<span class="keyword">switch</span> (dSelect)</span><br><span class="line">			&#123;</span><br><span class="line">			case1:</span><br><span class="line">				worker = <span class="keyword">new</span> Employee(newId, newName, dSelect);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			case2:</span><br><span class="line">				worker = <span class="keyword">new</span> Manager(newId, newName, dSelect);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">				worker = <span class="keyword">new</span> Boss(newId, newName, dSelect);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">default</span>:</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			<span class="comment">//更改数据 到数组中</span></span><br><span class="line">			<span class="keyword">this</span>-&gt;m_EmpArray[ret]= worker;</span><br><span class="line">			</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"修改成功！"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">			<span class="comment">//保存到文件中</span></span><br><span class="line">			<span class="keyword">this</span>-&gt;save();</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"修改失败，查无此人"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//按任意键 清屏</span></span><br><span class="line">	system(<span class="string">"pause"</span>);</span><br><span class="line">	system(<span class="string">"cls"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h6 id="12-3-测试修改职工"><a href="#12-3-测试修改职工" class="headerlink" title="12.3 测试修改职工"></a>12.3 测试修改职工</h6><p>在main函数分支 4  选项中，调用修改职工接口</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546502651922.png" alt></p>
<p>测试1 - 修改不存在职工情况</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546502759643.png" alt></p>
<p>测试2 - 修改存在职工情况，例如将职工 “李四” 改为 “赵四”</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546502830350.png" alt></p>
<p>修改后再次查看所有职工信息，并确认修改成功</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546502865443.png" alt></p>
<p>再次确认文件中信息也同步更新</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546502898653.png" alt></p>
<p>至此，修改职工功能已实现！</p>
<h4 id="13、查找职工"><a href="#13、查找职工" class="headerlink" title="13、查找职工"></a>13、查找职工</h4><p>功能描述：提供两种查找职工方式，一种按照职工编号，一种按照职工姓名</p>
<h6 id="13-1-查找职工函数声明"><a href="#13-1-查找职工函数声明" class="headerlink" title="13.1 查找职工函数声明"></a>13.1 查找职工函数声明</h6><p>在workerManager.h中添加成员函数  <code>void Find_Emp();</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//查找职工</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Find_Emp</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>



<h6 id="13-2-查找职工函数实现"><a href="#13-2-查找职工函数实现" class="headerlink" title="13.2 查找职工函数实现"></a>13.2 查找职工函数实现</h6><p>在workerManager.cpp中实现成员函数 <code>void Find_Emp();</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//查找职工</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">WorkerManager::Find_Emp</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>-&gt;m_FileIsEmpty)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"文件不存在或记录为空！"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"请输入查找的方式："</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"1、按职工编号查找"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"2、按姓名查找"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">int</span> select = <span class="number">0</span>;</span><br><span class="line">		<span class="built_in">cin</span> &gt;&gt; select;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> (select == <span class="number">1</span>) <span class="comment">//按职工号查找</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">int</span> id;</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"请输入查找的职工编号："</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">			<span class="built_in">cin</span> &gt;&gt; id;</span><br><span class="line"></span><br><span class="line">			<span class="keyword">int</span> ret = IsExist(id);</span><br><span class="line">			<span class="keyword">if</span> (ret != <span class="number">-1</span>)</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="built_in">cout</span> &lt;&lt; <span class="string">"查找成功！该职工信息如下："</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">				<span class="keyword">this</span>-&gt;m_EmpArray[ret]-&gt;showInfo();</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span></span><br><span class="line">			&#123;</span><br><span class="line">				<span class="built_in">cout</span> &lt;&lt; <span class="string">"查找失败，查无此人"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">if</span>(select == <span class="number">2</span>) <span class="comment">//按姓名查找</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">string</span> name;</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"请输入查找的姓名："</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">			<span class="built_in">cin</span> &gt;&gt; name;</span><br><span class="line"></span><br><span class="line">			<span class="keyword">bool</span> flag = <span class="literal">false</span>;  <span class="comment">//查找到的标志</span></span><br><span class="line">			<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m_EmpNum; i++)</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="keyword">if</span> (m_EmpArray[i]-&gt;m_Name == name)</span><br><span class="line">				&#123;</span><br><span class="line">					<span class="built_in">cout</span> &lt;&lt; <span class="string">"查找成功,职工编号为："</span></span><br><span class="line">                           &lt;&lt; m_EmpArray[i]-&gt;m_Id</span><br><span class="line">                           &lt;&lt; <span class="string">" 号的信息如下："</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">					</span><br><span class="line">					flag = <span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line">					<span class="keyword">this</span>-&gt;m_EmpArray[i]-&gt;showInfo();</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">if</span> (flag == <span class="literal">false</span>)</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="comment">//查无此人</span></span><br><span class="line">				<span class="built_in">cout</span> &lt;&lt; <span class="string">"查找失败，查无此人"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"输入选项有误"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	system(<span class="string">"pause"</span>);</span><br><span class="line">	system(<span class="string">"cls"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h6 id="13-3-测试查找职工"><a href="#13-3-测试查找职工" class="headerlink" title="13.3 测试查找职工"></a>13.3 测试查找职工</h6><p>在main函数分支 5  选项中，调用查找职工接口</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546504714318.png" alt></p>
<p>测试1 - 按照职工编号查找 - 查找不存在职工</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546504767229.png" alt></p>
<p>测试2 - 按照职工编号查找 -  查找存在职工</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546505046521.png" alt></p>
<p>测试3 - 按照职工姓名查找 - 查找不存在职工</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546505115610.png" alt></p>
<p>测试4 - 按照职工姓名查找 - 查找存在职工（如果出现重名，也一并显示，在文件中可以添加重名职工）</p>
<p>例如 添加两个王五的职工，然后按照姓名查找王五</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546507850441.png" alt></p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546507760284.png" alt></p>
<p>至此，查找职工功能实现完毕！</p>
<h4 id="14、排序"><a href="#14、排序" class="headerlink" title="14、排序"></a>14、排序</h4><p>功能描述：按照职工编号进行排序，排序的顺序由用户指定</p>
<h6 id="14-1-排序函数声明"><a href="#14-1-排序函数声明" class="headerlink" title="14.1 排序函数声明"></a>14.1 排序函数声明</h6><p>在workerManager.h中添加成员函数  <code>void Sort_Emp();</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//排序职工</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Sort_Emp</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>



<h6 id="14-2-排序函数实现"><a href="#14-2-排序函数实现" class="headerlink" title="14.2 排序函数实现"></a>14.2 排序函数实现</h6><p>在workerManager.cpp中实现成员函数 <code>void Sort_Emp();</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//排序职工</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">WorkerManager::Sort_Emp</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>-&gt;m_FileIsEmpty)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"文件不存在或记录为空！"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		system(<span class="string">"pause"</span>);</span><br><span class="line">		system(<span class="string">"cls"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"请选择排序方式： "</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"1、按职工号进行升序"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"2、按职工号进行降序"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">int</span> select = <span class="number">0</span>;</span><br><span class="line">		<span class="built_in">cin</span> &gt;&gt; select;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m_EmpNum; i++)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">int</span> minOrMax = i;</span><br><span class="line">			<span class="keyword">for</span> (<span class="keyword">int</span> j = i + <span class="number">1</span>; j &lt; m_EmpNum; j++)</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="keyword">if</span> (select == <span class="number">1</span>) <span class="comment">//升序</span></span><br><span class="line">				&#123;</span><br><span class="line">					<span class="keyword">if</span> (m_EmpArray[minOrMax]-&gt;m_Id &gt; m_EmpArray[j]-&gt;m_Id)</span><br><span class="line">					&#123;</span><br><span class="line">						minOrMax = j;</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">else</span>  <span class="comment">//降序</span></span><br><span class="line">				&#123;</span><br><span class="line">					<span class="keyword">if</span> (m_EmpArray[minOrMax]-&gt;m_Id &lt; m_EmpArray[j]-&gt;m_Id)</span><br><span class="line">					&#123;</span><br><span class="line">						minOrMax = j;</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			<span class="keyword">if</span> (i != minOrMax)</span><br><span class="line">			&#123;</span><br><span class="line">				Worker * temp = m_EmpArray[i];</span><br><span class="line">				m_EmpArray[i] = m_EmpArray[minOrMax];</span><br><span class="line">				m_EmpArray[minOrMax] = temp;</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"排序成功,排序后结果为："</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		<span class="keyword">this</span>-&gt;save();</span><br><span class="line">		<span class="keyword">this</span>-&gt;Show_Emp();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h6 id="14-3-测试排序功能"><a href="#14-3-测试排序功能" class="headerlink" title="14.3 测试排序功能"></a>14.3 测试排序功能</h6><p>在main函数分支 6  选项中，调用排序职工接口</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546510145181.png" alt></p>
<p>测试：</p>
<p>首先我们添加一些职工，序号是无序的，例如：</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546658169987.png" alt></p>
<p>测试 - 升序排序</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546658190479.png" alt></p>
<p>文件同步更新</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546658273581.png" alt></p>
<p>测试 - 降序排序</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546658288936.png" alt></p>
<p>文件同步更新</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546658313704.png" alt></p>
<p>至此，职工按照编号排序的功能实现完毕！</p>
<h4 id="15、清空文件"><a href="#15、清空文件" class="headerlink" title="15、清空文件"></a>15、清空文件</h4><p>功能描述：将文件中记录数据清空</p>
<h6 id="15-1-清空函数声明"><a href="#15-1-清空函数声明" class="headerlink" title="15.1 清空函数声明"></a>15.1 清空函数声明</h6><p>在workerManager.h中添加成员函数  <code>void Clean_File();</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//清空文件</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Clean_File</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>





<h6 id="15-2-清空函数实现"><a href="#15-2-清空函数实现" class="headerlink" title="15.2 清空函数实现"></a>15.2 清空函数实现</h6><p>在workerManager.cpp中实现员函数 <code>void Clean_File();</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//清空文件</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">WorkerManager::Clean_File</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"确认清空？"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"1、确认"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"2、返回"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">int</span> select = <span class="number">0</span>;</span><br><span class="line">	<span class="built_in">cin</span> &gt;&gt; select;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (select == <span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="comment">//打开模式 ios::trunc 如果存在删除文件并重新创建</span></span><br><span class="line">		<span class="function">ofstream <span class="title">ofs</span><span class="params">(FILENAME, ios::trunc)</span></span>;</span><br><span class="line">		ofs.<span class="built_in">close</span>();</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> (<span class="keyword">this</span>-&gt;m_EmpArray != <span class="literal">NULL</span>)</span><br><span class="line">		&#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>-&gt;m_EmpNum; i++)</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="keyword">if</span> (<span class="keyword">this</span>-&gt;m_EmpArray[i] != <span class="literal">NULL</span>)</span><br><span class="line">				&#123;</span><br><span class="line">					<span class="keyword">delete</span> <span class="keyword">this</span>-&gt;m_EmpArray[i];</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">this</span>-&gt;m_EmpNum = <span class="number">0</span>;</span><br><span class="line">			<span class="keyword">delete</span>[] <span class="keyword">this</span>-&gt;m_EmpArray;</span><br><span class="line">			<span class="keyword">this</span>-&gt;m_EmpArray = <span class="literal">NULL</span>;</span><br><span class="line">			<span class="keyword">this</span>-&gt;m_FileIsEmpty = <span class="literal">true</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"清空成功！"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	system(<span class="string">"pause"</span>);</span><br><span class="line">	system(<span class="string">"cls"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h6 id="15-3-测试清空文件"><a href="#15-3-测试清空文件" class="headerlink" title="15.3 测试清空文件"></a>15.3 测试清空文件</h6><p>在main函数分支 7  选项中，调用清空文件接口</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546511085541.png" alt></p>
<p>测试：确认清空文件</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546510976745.png" alt></p>
<p>再次查看文件中数据，记录已为空</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546510994196.png" alt></p>
<p>打开文件，里面数据已确保清空，该功能需要慎用！</p>
<p><img src="/2020/04/29/Cpp/05.%20%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/1546511018517.png" alt></p>
<p>随着清空文件功能实现，本案例制作完毕  ^ _ ^  </p>

    </div>
    <p class="post-meta">
        <span class="post-cat">分类：
            <a class="cat-link" href="/categories/%E5%90%8E%E7%AB%AF/">后端</a>
        </span>
        <span class="post-tags">
            标签：
            
    
        <a href="/tags/cpp/" title="cpp">cpp</a>
    

        </span>
    </p>
</article>
<!-- 分享按钮 -->

  <div class="article-share clearfix text-center">
    <div class="share-area">
      <span class="share-txt">分享到：</span>
      <a href="javascript: window.open('http://service.weibo.com/share/share.php?url=' + encodeURIComponent(location.href) + '&title=' + document.title + '&language=zh_cn');" class="share-icon weibo"></a>
      <a href="javascript: alert('请复制链接到微信并发送');" class="share-icon wechat"></a>
      <a href="javascript: window.open('http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=' + encodeURIComponent(location.href) + '&title=' + document.title);" class="share-icon qqzone"></a>
      <a href="javascript: window.open('http://connect.qq.com/widget/shareqq/index.html?url=' + encodeURIComponent(location.href) + '&desc=AllVar个人博客&title=' + document.title + '&callback=' + encodeURIComponent(location.href));" class="share-icon qq"></a>
      <a href="javascript: window.open('http://shuo.douban.com/!service/share?href=' + encodeURIComponent(location.href) + '&name=' + document.title + '&text=' + document.title);" class="share-icon douban"></a>
    </div>
  </div>


<!-- 上一篇/下一篇 -->

<div class="article-nav clearfix">
    
    <span class="prev fl">
        上一篇<br >
        <a href="/2020/04/29/Cpp/06.%20%E7%BC%96%E7%A8%8B%E8%BF%9B%E9%98%B6/">
            
                编程进阶
            
        </a>
    </span>
    

    
    <span class="next fr">
        下一篇<br >
        <a href="/2020/04/29/Cpp/04.%20%E6%A0%B8%E5%BF%83%E7%BC%96%E7%A8%8B/">
            
                核心编程
            
        </a>
    </span>
    
</div>

<!-- 文章评论 -->


            </div>

        </section>
        <!-- 侧栏部分 -->
<aside class="sidebar">
    
    <section class="widget">
        <h3 class="widget-hd"><strong>文章搜索</strong></h3>
        <div class="search-form">
  <form
    id="searchForm"
    method="GET"
    action="https://www.baidu.com/s"
    ectype="application/x-www-form-urlencoded"
    target="_blank"
    autocomplete="false"
    onsubmit="javascript: return false;">
    <input
      id="searchKeyword"
      type="text"
      class="form-control"
      placeholder="输入关键字搜索"
      autocomplete="false"
    />
    <input id="searchKeywordHidden" type="hidden" name="wd" />
    <input id="searchButton" class="btn" type="submit" value="搜索" />
  </form>
</div>
    </section>
    

    <section class="widget">
        <h3 class="widget-hd"><strong>文章分类</strong></h3>
        <!-- 文章分类 -->
<ul class="widget-bd">
    
    <li>
        <a href="/categories/%E5%90%8E%E7%AB%AF/">后端</a>
        <span class="badge">(20)</span>
    </li>
    
    <li>
        <a href="/categories/%E5%89%8D%E7%AB%AF/">前端</a>
        <span class="badge">(3)</span>
    </li>
    
    <li>
        <a href="/categories/%E5%B7%A5%E5%85%B7/">工具</a>
        <span class="badge">(2)</span>
    </li>
    
</ul>
    </section>

    
    <section class="widget">
        <h3 class="widget-hd"><strong>热门标签</strong></h3>
        <!-- 文章标签 -->
<div class="widget-bd tag-wrap">
  
    <a class="tag-item" href="/tags/centeros/" title="centeros">centeros (4)</a>
  
    <a class="tag-item" href="/tags/python/" title="python">python (1)</a>
  
    <a class="tag-item" href="/tags/docker/" title="docker">docker (6)</a>
  
    <a class="tag-item" href="/tags/cpp/" title="cpp">cpp (8)</a>
  
    <a class="tag-item" href="/tags/dotnet-core/" title="dotnet core">dotnet core (1)</a>
  
    <a class="tag-item" href="/tags/nginx/" title="nginx">nginx (1)</a>
  
    <a class="tag-item" href="/tags/redis/" title="redis">redis (2)</a>
  
    <a class="tag-item" href="/tags/unity/" title="unity">unity (3)</a>
  
    <a class="tag-item" href="/tags/c/" title="c#">c# (3)</a>
  
    <a class="tag-item" href="/tags/vue/" title="vue">vue (3)</a>
  
    <a class="tag-item" href="/tags/javascript/" title="javascript">javascript (3)</a>
  
    <a class="tag-item" href="/tags/visual-studio/" title="visual studio">visual studio (1)</a>
  
    <a class="tag-item" href="/tags/tool/" title="tool">tool (2)</a>
  
    <a class="tag-item" href="/tags/vscode/" title="vscode">vscode (1)</a>
  
</div>
    </section>
    

    
    <!-- 友情链接 -->
    <section class="widget">
        <h3 class="widget-hd"><strong>友情链接</strong></h3>
        <!-- 文章分类 -->
<ul class="widget-bd">
    
        <li>
            <a href="https://gitee.com/Shr9527" target="_blank" title="码云博客">码云博客</a>
        </li>
    
</ul>
    </section>
    
</aside>
<!-- / 侧栏部分 -->
    </div>

    <!-- 博客底部 -->
    <footer class="footer">
    &copy;
    
        2016-2020
    

    <a href="/">AllVar Loves You</a>
</footer>
<div class="back-to-top" id="ALLVAR__backToTop" title="返回顶部">返回顶部</div>

    <!--博客js脚本 -->
    <!-- 这里放网站js脚本 -->

<script src="/js/main.js?v=1590833788391.js"></script>

</body>
</html>